<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" tppabs="http://res.layui.com/layui/dist/css/layui.css"
          media="all">
    <link rel="stylesheet" href="../layui/css/public.css" media="all" />
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<br/>
<br/>


<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">搜索框1</label>
            <div class="layui-input-inline">
                <input type="text" name="cartype" placeholder="请输入xx" class="layui-input">
            </div>

            <label class="layui-form-label">搜索框2</label>
            <div class="layui-input-inline">
                <input type="text" name="cartype1" placeholder="请输入xx" class="layui-input">
            </div>

            <label class="layui-form-label">搜索框3</label>
            <div class="layui-input-inline">
                <input type="text" name="cartype2" placeholder="请输入xx" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" id="doSearch">查询</button>
            <button type="reset" class="layui-btn layui-btn-warm  layui-icon layui-icon-refresh">重置</button>
        </div>
    </div>


</form>


<table class="layui-hide" id="test" lay-filter="test"></table>
</body>


<script src="../layui/layui.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="">头部工具栏1</button>
        <button class="layui-btn layui-btn-sm" lay-event="">头部工具栏2</button>
        <button class="layui-btn layui-btn-sm" lay-event="">头部工具栏3</button>
    </div>
</script>

<script type="text/html" id="barDemo" charset="UTF-8">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="">按钮2</a>
</script>

<script>
    //编辑动态表格遍历数组
    layui.use('table', function () {
        var table = layui.table;
        table.render({
            elem: '#test'
            , url: 'busCar/getAllCar'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '车库信息'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'identity', title: '身份证号', edit:'text' , sort: true,align: 'center'}
                , {field: 'custname', title: '姓名',  edit: 'text',sort: true,align: 'center'}
                , {field: 'sex', title: '性别', edit: 'text',sort: true,align: 'center'}
                , {field: 'address', title: '地址',edit: 'text',sort: true,align: 'center'}
                , {field: 'price', title: '联系方式',edit: 'text',sort: true,align: 'center'}
                , {field: 'career', title: '职业', edit: 'text',sort: true,align: 'center'}
                , {
                    field: 'createtime', title: '注册日期',sort: true,align: 'center',
                    templet: "<div>{{layui.util.toDateString(d.createtime,'yyyy-MM-dd')}}</div>"
                }
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
            ]]
            , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                , curr: 1 //设定初始在第 1 页
                , groups: 3 //只显示 3 个连续页码
                , first: false //不显示首页
                , last: false //不显示尾页
                , limit: 3
                , limits: [1, 2, 3]

            }
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                //工具栏1 lay-event 自行编辑
                case '':

                    break;
                //工具栏2
                case '':

                    break;
                //工具栏3
                case '':

                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
            ;
        });

        //监听行工具事件
        //按钮1 lay-event 自行编辑
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'edit') {
                queryOne(data);
            } else if (obj.event === '这里写按钮2的lay-event事件') {

            }
        });
    });
</script>


<script type="application/javascript">

    function queryOne(data) {
        layer.open({
            type: 1,//页面层
            title: ['修改', 'font-size:18px;'],
            area: ['600px', '600px'],
            content: $("#updateForm").html(), //这里content是一个普通的String

            //函数
            //弹出后回调
            success: function () {
                //layer.alert(JSON.stringify(data))
                $("#identity").val(data.identity);
                $("#custname").val(data.custname);
                //性别
                if ("男" == data.sex) {
                    $("#sex").append("<input type='radio' name='sex' value='男' title='男' checked><input type='radio' name='sex' value='女' title='女'>")
                } else {
                    $("#sex").append("<input type='radio' name='sex' value='男' title='男' ><input type='radio' name='sex' value='女' title='女' checked>")
                }
                $("#address").val(data.address);
                $("#phone").val(data.phone);
                $("#career").val(data.career);
                $("#createtime").val(layui.util.toDateString(data.createtime, 'yyyy-MM-dd'));

                //渲染页面
                layui.form.render();

            }
            , btn: ['提交', '取消']
            , yes: function (index, layero) {
                //alert("提交")
                //调用后台修改的方法，使用ajax
                $.ajax({
                    type: "POST",
                    //记得改路径
                    url: "user/update",
                    data: $("#update").serialize(),
                    success: function (backData) {
                        if ("success" == backData) {
                            location.reload();
                        }
                    }
                });


            }
            , btn2: function (index, layero) {
                //alert("取消")
            }

        });
    }


</script>


<script type="text/html" id="updateForm">
    <form class="layui-form" id="update">
        <!-- id作为隐藏域传到后台 -->
        <input type="hidden" name="id" id="id">

        <div class="layui-form-item">
            <label class="layui-form-label">身份证</label>
            <div class="layui-input-block">
                <input type="text" name="identity" id="identity" required lay-verify="required" placeholder="请输入用户名"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-inline">
                <input type="text" name="custname" id="custname" required lay-verify="required" placeholder="请输入密码"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-inline" id="userGender">

            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">地址</label>
            <div class="layui-input-inline">
                <input type="text" name="address" id="address" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">联系方式</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" id="phone" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">职业</label>
            <div class="layui-input-inline">
                <input type="text" name="career" id="career" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">注册日期</label>
                <div class="layui-input-inline">
                    <input type="date" class="layui-input" name="createtime" id="createtime">
                </div>
            </div>
        </div>

    </form>
</script>

</html>